<?php

class Lib_Repair_Base_Tool
{

    private static $pre_fix = 'sms_check_scene_';

    /**
     * 过滤SPAM垃圾短信
     * @param $scene
     * @param $mobile
     * @return bool|int
     */
    public static function check_spam($scene, $mobile)
    {
        //验证发送短信间隔时间 60秒
        $key = self::$pre_fix . $scene . '_mobile_' . $mobile;
        $value = LrFunc_Cachefunc::getInstance()->get($key);
        if ($value) {
            return 5502;
        }

        //验证发送次数，每天限制10条
        $res = LrFunc_Redisfunc::getInstance()->checkceiling('sms', $mobile, 10);
        if ($res === false) {
            return 5501;
        }
        LrFunc_Cachefunc::getInstance()->set($key, time(), 60);

        return true;

    }

    /**
     * 验证QPS
     * @return bool|int
     */
    public static function check_QPS($log)
    {
        if(PHP_SAPI === 'cli'){
            return true;
        }
        //验证发送短信间隔时间 1秒
        $key = self::$pre_fix . "QPS";
        //获取当前吞吐量
        $num = Lib_Redis_String::get($key);
        if($num > 20){
            //报警
            $address = [
                'tangchunxin@p2peye.com',
                'weijinguo@p2peye.com',
                'fubaosheng@p2peye.com',
                'tianweiying@p2peye.com',
                'linhaowei@p2peye.com',
            ];
            $subject = date('Y-m-d', time()) . '短信被刷报警';
            $body = '<p>短信被刷报警次数20/秒</p >';
            $body .= '</br>params: <p>'.$log.'</p >';
            $body .= '</br>URL: <p>'.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'</p >';
            foreach ($address as $value) {
                $res = Lib_Postman::sendMail($value, $subject, $body);
            }
            return 5505;
        }
        $obj = new Lib_Redis_String;
        if(!$num){
            $obj->setex($key, 1, 1);

        }else{
            $obj->incr($key);
        }
        unset($obj);
      /*  //自增1
        $obj->incr($key);
        if(!$num){
            Lib_Redis_String::setTimeout($key, 1);
        }*/
        return true;

    }

    /**
     * 手机黑名单校验
     * @param $mobile
     * @return bool|int
     */
    public static function check_black($mobile)
    {
        $black_mobile = Lib_Repair_Config::$black_mobile;
        if (in_array($mobile, $black_mobile)) {
            return 5503;
        }
        return true;
    }


    /**
     * 格式化短信内容
     * @param $mould_txt
     * @param array $argv
     * @return mixed
     * @throws Exception
     */
    public static function formatText($mould_txt, $argv = array())
    {

        preg_match_all("/\{.*\}/U", $mould_txt, $res_arr);

        //正则发生错误
        if ($res_arr === false) {
            throw new Exception('Message content for error',4005);
        }

        //短信参数必须与配置数量相等
        if (count($res_arr[0]) != count($argv)) {
            throw new Exception('Message call parameter error',4006);
        }

        foreach ($argv as $v) {
            $mould_txt = preg_replace("/\{.*?\}/", $v, $mould_txt, 1);
        }

        return $mould_txt;
    }

    public static function checkData($data,$fields){
        $check = true;
        foreach($fields as $val){
            if(!isset($data[$val])){
                $check = false;
                break;
            }

        }
        return $check;
    }


    //获取随机字符串
    public static function getNonce(){
//        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
//        $charid = strtoupper(md5(uniqid(rand(), true)));
        return time() . rand();;
    }
    //获取毫秒时间戳
    public static function getMillisecond(){
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectimes = substr($msectime,0,13);
    }

    //解成16进制
    public static function Hex2String($hex){
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2){
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }


    //加密
    public static function encrypt($data,$key) {
        $data =  openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
        return bin2hex($data);
    }
    //解密
    public static function decrypt($data,$key) {
        $encrypted = base64_decode($data);
        return openssl_decrypt($encrypted, 'aes-128-ecb',  self::Hex2String($key), OPENSSL_RAW_DATA);
    }
}